package com.suse.warmstill.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.suse.warmstill.common.enums.ResponseEnum;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.service.RoleService;
import com.suse.warmstill.vo.ConditionVO;
import com.suse.warmstill.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 黄靖
 * @author 2022-10-20 00:40
 * description:
 **/

@CrossOrigin
@RestController
@RequestMapping("/warmstill/roles")
@Api("角色表的操作,，部门老大可以操作")
public class PCRoleApi {

    @Autowired
    private RoleService roleService;

    /*
     * 分页查询全部角色
     * */
    // @RoleCheck(module ="分页查询全部角色",limitedRoleCodeList ="[222]")
    @ApiOperation("根据 rolename 分页查询所有的 角色,返回 menu集合 resource 集合")
    @GetMapping("/listRole")
    public CallResult<ListPageModel<RoleVo>> listRoles(@Validated ConditionVO conditionVO) {
        ListPageModel<RoleVo> page = roleService.listRoles(conditionVO);
        return CallResult.success(page);
    }
    /*
     * 删除 角色(逻辑删除)
     * */

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation(value = "根据 角色id集合 禁用角色，以及相关的关联表,传入 类型 List<Long>")
    @PutMapping("/deleteRole")
    public CallResult<Void> disableRoles(Long roleId) {
        if (
                ObjectUtils.isEmpty(roleId)
        ) {
            throw new BusinessException(ResponseEnum.PARAM_ISNULL.getCode(), ResponseEnum.PARAM_ISNULL.getMsg());
        } else {
            roleService.disableRole(roleId);
            return CallResult.success();
        }
    }

    /*
     * 修改角色，以及对应的menu
     * */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("修改角色，以及对应的menu")
    @PostMapping("/updateRoleMenu")
    public CallResult<Void> updateRoleMenu(@RequestBody RoleVo roleVo) {
        roleService.updateRoleMenu(roleVo);

        return CallResult.success();
    }

    /*
     * 修改角色 以及对应的 resource
     * */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("修改角色，以及对应的resource")
    @PutMapping("/updateRoleResource")
    public CallResult<Void> updateRoleResource(RoleVo roleVo) {
        roleService.updateRoleResource(roleVo);

        return CallResult.success();
    }

    /*
     * 新增角色
     * */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("新增角色，也会新增 前端资源 和 菜单")
    @PostMapping("/insertRole")
    public CallResult<Void> insertRole(@RequestBody @Validated RoleVo roleVo) {
        roleService.insertRole(roleVo);
        return CallResult.success();
    }


}
